#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStringList>
#include <QMessageBox>
#include <QFileDialog>
#include <QThread>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    xthread = new QThread();

    Mutex = new QMutex(QMutex::NonRecursive);

    DownLoadTaskHandler = new DownloadThread(&StFlag,Mutex);
    DownLoadTaskHandler->moveToThread(xthread);
    xthread->start();

    connect(this,&MainWindow::onDownLoad_Handler,DownLoadTaskHandler,&DownloadThread::DownLoad_Handler);
    connect(DownLoadTaskHandler,&DownloadThread::SeriaPortOff,this,&MainWindow::SeriaPortOff);
    connect(DownLoadTaskHandler,&DownloadThread::SeriaPortON,this,&MainWindow::SeriaPortON);
    connect(DownLoadTaskHandler,&DownloadThread::File_NotExist,this,&MainWindow::File_NotExist);
    connect(DownLoadTaskHandler,&DownloadThread::File_OpenError,this,&MainWindow::File_OpenError);
    connect(DownLoadTaskHandler,&DownloadThread::SendingProgress,this,&MainWindow::SendingProgress);
    connect(DownLoadTaskHandler,&DownloadThread::DownLoad_IsSuccess,this,&MainWindow::DownLoad_IsSuccess);
    connect(this,&MainWindow::onOpenSeriaPort,DownLoadTaskHandler,&DownloadThread::OpenSeriaPort);

    TFPort = new Thread_FreshPort(ui->comboBox);
    connect(TFPort,&Thread_FreshPort::FreshPort,this,[this](QStringList slist){
        ui->comboBox->clear();
        for(QString PortName : slist)
        {
            ui->comboBox->addItem(PortName);
        }
    });

    TFPort->start();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::DownLoad_IsSuccess(int reply)
{
    if(reply == 0)
        QMessageBox::information(nullptr,"下载成功","程序已下载");
    else if(reply == -1)
        QMessageBox::warning(this,"警告","发送错误，请检查通讯");
    else if (reply == -2)
        QMessageBox::warning(this,"警告","接收方终止");
    else if(reply == 1)
        QMessageBox::information(nullptr,"传输终止","传输已终止");
    ui->btn_Download->setEnabled(true);
    ui->btn_connect->setEnabled(true);
}

void MainWindow::SendingProgress(int value)
{
    ui->progressBar->setValue(value);
}

void MainWindow::File_NotExist(void)
{
    QMessageBox::warning(this,"警告","文件不存在，请检查路径");
}

void MainWindow::File_OpenError(void)
{
    QMessageBox::warning(this,"警告","文件打开失败");
}

void MainWindow::on_btn_connect_clicked()
{
    emit onOpenSeriaPort(ui->comboBox->currentText());
}

void MainWindow::on_btn_Download_clicked()
{
    ui->btn_Download->setEnabled(false);
    ui->btn_connect->setEnabled(false);
    ui->progressBar->setValue(0);
    Mutex->lock();
    StFlag = 0;
    Mutex->unlock();
    emit onDownLoad_Handler(ui->lineEdit_filepath->text());
}

void MainWindow::SeriaPortOff(void)
{
    ui->btn_connect->setText("打开串口");
    ui->comboBox->setEnabled(true);
    ui->btn_Download->setEnabled(false);
}
void MainWindow::SeriaPortON(void)
{
    ui->btn_connect->setText("关闭串口");
    ui->comboBox->setEnabled(false);
    ui->btn_Download->setEnabled(true);
}

void MainWindow::on_toolButton_clicked()
{
    QString filepath = QFileDialog::getOpenFileName(this,tr("选择下载程序"),"C:/Users/Administrator/Desktop",tr("*.bin"));
    ui->lineEdit_filepath->setText(filepath);
}

void MainWindow::on_pushButton_clicked()
{
    Mutex->lock();
    ui->btn_Download->setEnabled(true);
    ui->btn_connect->setEnabled(true);
    StFlag = 1;
    Mutex->unlock();
}
